package com.neuedu.hisboot.controller;

import com.neuedu.hisboot.dto.Result;
import com.neuedu.hisboot.entity.PatientInfo;
import com.neuedu.hisboot.service.IPatientInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 病人详细信息管理控制器
 * </p>
 *
 * @author jaun
 * @since 2025-07-04
 */
@RestController
@RequestMapping("/patientinfo")
public class PatientInfoController {

    @Autowired
    private IPatientInfoService patientInfoService;

    /**
     * 获取所有病人详细信息
     */
    @GetMapping("/list")
    public Result<List<PatientInfo>> getPatientInfos() {
        List<PatientInfo> patients = patientInfoService.list();
        return Result.success(patients);
    }

    /**
     * 根据ID获取病人详细信息
     */
    @GetMapping("/{id}")
    public Result<PatientInfo> getPatientInfoById(@PathVariable("id") Integer id) {
        PatientInfo patient = patientInfoService.getById(id);
        if (patient == null) {
            return Result.error(500, "未找到对应的病人信息");
        }
        return Result.success(patient);
    }

    /**
     * 根据病历号获取病人信息
     */
    @GetMapping("/mrid/{mrid}")
    public Result<PatientInfo> getPatientInfoByMrid(@PathVariable("mrid") String mrid) {
        PatientInfo patient = patientInfoService.getByMrid(mrid);
        if (patient == null) {
            return Result.error(500, "未找到对应的病人信息");
        }
        return Result.success(patient);
    }

    /**
     * 根据身份证号获取病人信息
     */
    @GetMapping("/idcard/{idCardNo}")
    public Result<PatientInfo> getPatientInfoByIdCard(@PathVariable("idCardNo") String idCardNo) {
        PatientInfo patient = patientInfoService.getByIdCard(idCardNo);
        if (patient == null) {
            return Result.error(500, "未找到对应的病人信息");
        }
        return Result.success(patient);
    }

    /**
     * 根据住院号获取病人信息
     */
    @GetMapping("/hospitalization/{hospitalizationNumber}")
    public Result<PatientInfo> getPatientInfoByHospitalizationNumber(@PathVariable("hospitalizationNumber") String hospitalizationNumber) {
        PatientInfo patient = patientInfoService.getByHospitalizationNumber(hospitalizationNumber);
        if (patient == null) {
            return Result.error(500, "未找到对应的病人信息");
        }
        return Result.success(patient);
    }

    /**
     * 创建新的病人详细信息
     */
    @PostMapping("/create")
    public Result<PatientInfo> createPatientInfo(@RequestBody PatientInfo patientInfo) {
        try {
//            if (patientInfo.getDelMark() == null) {
//                patientInfo.setDelMark(1);
//            }
            boolean success = patientInfoService.save(patientInfo);
            if (success) {
                return Result.success(patientInfo);
            } else {
                return Result.error(500, "创建病人信息失败");
            }
        } catch (Exception e) {
            return Result.error(500, "创建病人信息时发生错误: " + e.getMessage());
        }
    }

    /**
     * 更新病人详细信息
     */
    @PutMapping("/update")
    public Result<PatientInfo> updatePatientInfo(@RequestBody PatientInfo patientInfo) {
        try {
            boolean success = patientInfoService.updateById(patientInfo);
            if (success) {
                return Result.success(patientInfo);
            } else {
                return Result.error(500, "更新病人信息失败");
            }
        } catch (Exception e) {
            return Result.error(500, "更新病人信息时发生错误: " + e.getMessage());
        }
    }

    /**
     * 删除病人详细信息（逻辑删除）
     */
    @DeleteMapping("/{id}")
    public Result<String> deletePatientInfo(@PathVariable("id") Integer id) {
        try {
            PatientInfo patient = patientInfoService.getById(id);
            if (patient == null) {
                return Result.error(500, "未找到对应的病人信息");
            }
            
            // 逻辑删除
//            patient.setDelMark(0);
            boolean success = patientInfoService.updateById(patient);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error(500, "删除病人信息失败");
            }
        } catch (Exception e) {
            return Result.error(500, "删除病人信息时发生错误: " + e.getMessage());
        }
    }

    /**
     * 根据科室获取病人信息
     */
    @GetMapping("/department/{departmentName}")
    public Result<List<PatientInfo>> getPatientInfosByDepartment(@PathVariable("departmentName") String departmentName) {
        List<PatientInfo> patients = patientInfoService.getByDepartment(departmentName);
        return Result.success(patients);
    }

    /**
     * 根据医生获取病人信息
     */
    @GetMapping("/doctor/{doctorName}")
    public Result<List<PatientInfo>> getPatientInfosByDoctor(@PathVariable("doctorName") String doctorName) {
        List<PatientInfo> patients = patientInfoService.getByDoctor(doctorName);
        return Result.success(patients);
    }

    /**
     * 根据疾病获取病人信息
     */
    @GetMapping("/illness/{illness}")
    public Result<List<PatientInfo>> getPatientInfosByIllness(@PathVariable("illness") String illness) {
        List<PatientInfo> patients = patientInfoService.getByIllness(illness);
        return Result.success(patients);
    }
}

